<?php
class Mozg_Lib_Dir implements Mozg_Lib
{
    public function Create($dir)
    {
        $dir = rtrim($dir, '.');
        if ($this->exists($dir)) return true;

        $umask = umask(0);
        $result = mkdir($dir, _CHMOD_DIR, true);
        umask($umask);
        return $result;
    }

    public function exists($dir)
    {
        // no file requested?
        $dir = trim($dir);
        if (! $dir) {
            return false;
        }

        // using an absolute path for the file?
        // dual check for Unix '/' and Windows '\',
        // or Windows drive letter and a ':'.
        $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':');
        if ($abs && is_dir($dir)) {
            return $dir;
        }
        /*
        // using a relative path on the file
        $path = explode(PATH_SEPARATOR, ini_get('include_path'));
        foreach ($path as $base) {
            // strip Unix '/' and Windows '\'
            $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir;
            if (is_dir($target)) {
                return $target;
            }
        }
        */
        // never found it
        return false;
    }

    public function TestExtension($filePath, $ext = '')
    {
    	$fileExt = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
    	$ext = array_map('strtolower', (array)$ext);
    	return in_array($fileExt, $ext);
    }

    public function DelFilesByType($dir, $exts = array())
    {
        $totalFiles = 0;

        $dir = rtrim($dir, '/');
        if (!is_dir($dir)) return true;
        if($handler = opendir($dir))
        {
            while (($sub = readdir($handler)) !== FALSE)
            {
                if ($sub == "." || $sub == "..") continue;

                $totalFiles++;

                if(is_file($dir."/".$sub))
                {
                    if ($this->TestExtension($sub, $exts))
                    {
                        if (unlink($dir."/".$sub)) $totalFiles--;
                    }
                }
                elseif(is_dir($dir."/".$sub))
                {
                    if ($this->DelFilesByType($dir."/".$sub, $exts)) $totalFiles--;
                }
            }
            closedir($handler);
            if (!$totalFiles && rmdir($dir)) return true;
        }
    }

    public function MoveUploadedFile($path, $newPath)
    {
		if (!is_file($path)) return false;

		$newPathFolder = pathinfo($newPath, PATHINFO_DIRNAME);
		if (!$this->Create($newPathFolder)) return false;

		return copy($path, $newPath);
    }

    public function MoveFromTempFolder($path)
    {
    	if (!strlen($path) || !is_file(_ROOT_PATH.$path)) return false;

    	$tempFolder = Mozg_Cfg::Get('global')->GetTempFolder();
		if (strpos($path, $tempFolder) !== 0) return $path;

		$newPath = substr($path, Mozg::Lib('Str')->Len($tempFolder));
		$newFolder = pathinfo($newPath, PATHINFO_DIRNAME);
		if (!$this->Create(_ROOT_PATH.$newFolder)) return $path;

		if (copy(_ROOT_PATH.$path, _ROOT_PATH.$newPath))
		{
			@unlink(_ROOT_PATH.$path);
			return $newPath;
		}

		return $path;
    }
}